#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cstdio>
#include <allegro5/allegro.h>
#include <allegro5/allegro_opengl.h>

#include "ExtGL.h"




using namespace std;

volatile bool closed=false;


void handler(){
	closed=true;
}




void redraw(){

//	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//
//		    glBegin(GL_QUADS);
//		        glColor3d(1,0,0);
//		        glVertex3f(-1,-1,-10);
//		        glColor3d(1,1,0);
//		        glVertex3f(1,-1,-10);
//		        glColor3d(1,1,1);
//		        glVertex3f(1,1,-10);
//		        glColor3d(0,1,1);
//		        glVertex3f(-1,1,-10);
//		    glEnd();


	//drawCube(20, 20, 0);


	ExtGL::drawCube(20, 20, 0);


//	glPushMatrix();
//	 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//	glMatrixMode(GL_MODELVIEW);
//	glLoadIdentity();
//	glBegin(GL_TRIANGLES);
//	glColor3f(1.0f,1.0f,1.0f);
//	 glVertex3f(0.0f,0.0f,0.0f);
//	 glVertex3f(800.0f,0.0f,0.0f);
//	glVertex3f(800.0f,600.0f,0.0f);
//	 glEnd();
//	glFlush();
//	glPopMatrix();


	 al_flip_display();


}


void initGl(){
	glEnable(GL_DEPTH_TEST);
//	glEnable(GL_DEPTH);
	glViewport(0,0,640,480);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0,800,600,0,-200,200);
	glClearDepth(1.0f);
	glClearColor(0,0,0,1);
//	   glEnable(GL_DEPTH_TEST);
//	  glEnable(GL_NORMALIZE);

	 glClearColor(0,0,0,0);
}



int main(int argc, char **argv){

   ALLEGRO_DISPLAY *display = NULL;
   ALLEGRO_EVENT_QUEUE *event_queue = NULL;

   if(!al_init()) {
      fprintf(stderr, "failed to initialize allegro!\n");
      return -1;
   }

  // al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_OPENGL_3_0 | ALLEGRO_OPENGL_FORWARD_COMPATIBLE);

   al_set_new_display_option(ALLEGRO_DEPTH_SIZE,24,ALLEGRO_REQUIRE); // pentru depth

   display = al_create_display(640, 480);
   if(!display) {
         fprintf(stderr, "failed to create display!\n");
         return -1;
      }

   event_queue = al_create_event_queue();
   if(!event_queue) {
         fprintf(stderr, "failed to create event_queue!\n");
         al_destroy_display(display);
         return -1;
    }







   al_register_event_source(event_queue, al_get_display_event_source(display));



   initGl();


  al_set_window_title(display, "Hello world");

//   al_clear_to_color(al_map_rgb(255,0,0));

   al_flip_display();


   while(1)
      {
         ALLEGRO_EVENT ev;
         ALLEGRO_TIMEOUT timeout;
         al_init_timeout(&timeout, 0.06);

         bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout);

         if(get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
        	 	 printf("sdadasdasd");
        	 	 std::exit(2);
            break;
         }

         else if(get_event && ev.type == ALLEGRO_EVENT_KEY_DOWN){
        	 printf("%i", ev.keyboard.keycode);
         }


         else{
        	 redraw();
         }

//         al_clear_to_color(al_map_rgb(0,220,20));
//         al_flip_display();
      }


   al_rest(10.0);

   al_destroy_display(display);

   return 0;
}
